package com.jhgsys.internal.hazard.service.impl;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.jhgsys.internal.common.entity.FebsConstant;
import com.jhgsys.internal.common.utils.BeanHelper;
import com.jhgsys.internal.hazard.dto.HazardExamineObjectDto;
import com.jhgsys.internal.hazard.dto.HazardExamineRecordDto;
import com.jhgsys.internal.hazard.entity.HazardExamineObject;
import com.jhgsys.internal.hazard.entity.HazardExamineRecord;
import com.jhgsys.internal.hazard.mapper.HazardExamineObjectMapper;
import com.jhgsys.internal.hazard.service.IHazardExamineObjectService;
import com.jhgsys.internal.common.entity.QueryRequest;
import com.jhgsys.internal.common.utils.FebsUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.annotation.Propagation;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.toolkit.StringPool;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 *  Service实现
 *
 * @author admin
 * @date 2024-08-21 18:00:00
 */
@Service
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class HazardExamineObjectServiceImpl extends ServiceImpl<HazardExamineObjectMapper, HazardExamineObject> implements IHazardExamineObjectService {

    /**
     * 查询（分页）
     * @author admin
     * @date 2024-08-21 18:00:00
     * @param request QueryRequest
     * @param hazardExamineObject hazardExamineObject
     * @return IPage<HazardExamineObject>
    */
    @Override
    public IPage<HazardExamineObject> findHazardExamineObjectPage(QueryRequest request, HazardExamineObject hazardExamineObject) {
        LambdaQueryWrapper<HazardExamineObject> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(HazardExamineObject::getDeletedFlg, 0);
        // TODO 设置查询条件

        Page<HazardExamineObject> page = new Page<>(request.getPageNum(), request.getPageSize());
        FebsUtil.pageWrapper(page, request);
        return this.page(page, queryWrapper);
    }

    /**
     * 查询（所有）
     * @author admin
     * @date 2024-08-21 18:00:00
     * @param hazardExamineObject hazardExamineObject
     * @return List<HazardExamineObject>
     */
    @Override
    public List<HazardExamineObject> findHazardExamineObjectList(HazardExamineObject hazardExamineObject) {
	    LambdaQueryWrapper<HazardExamineObject> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(HazardExamineObject::getDeletedFlg, FebsConstant.VALID);
		// TODO 设置查询条件
        //检查id
        if(StringUtils.isNotBlank(hazardExamineObject.getExamineId())){
            queryWrapper.eq(HazardExamineObject::getExamineId, hazardExamineObject.getExamineId());
        }
		return this.baseMapper.selectList(queryWrapper);
    }

    /**
    * 根据ID查询
    * @author admin
    * @date 2024-08-21 18:00:00
    * @param id
    * @return HazardExamineObject
    */
    @Override
    public HazardExamineObject findHazardExamineObjectById(String id){
        return this.baseMapper.selectById(id);
    }

    /**
     * 新增
     * @author admin
     * @date 2024-08-21 18:00:00
     * @param hazardExamineObject hazardExamineObject
     */
    @Override
    @Transactional
    public void createHazardExamineObject(HazardExamineObject hazardExamineObject) {
        hazardExamineObject.setCreatedTime(new Date());
        this.save(hazardExamineObject);
    }

    /**
     * 修改
     * @author admin
     * @date 2024-08-21 18:00:00
     * @param hazardExamineObject hazardExamineObject
     */
    @Override
    @Transactional
    public void updateHazardExamineObject(HazardExamineObject hazardExamineObject) {
        hazardExamineObject.setUpdatedTime(new Date());
        this.saveOrUpdate(hazardExamineObject);
    }

    /**
     * 逻辑删除
     * @author admin
     * @date 2024-08-21 18:00:00
     * @param hazardExamineObject hazardExamineObject
     * @param hazardExamineObjectIds hazardExamineObjectIds
     */
    @Override
    @Transactional
    public void deleteHazardExamineObject(HazardExamineObject hazardExamineObject,String hazardExamineObjectIds) {
        List<String> list = Arrays.asList(hazardExamineObjectIds.split(StringPool.COMMA));
        this.baseMapper.update(hazardExamineObject,new LambdaQueryWrapper<HazardExamineObject>().in(HazardExamineObject::getExamineObjectId,list));
	}

    /**
    * 物理删除
    * @author admin
    * @date 2024-08-21 18:00:00
    * @param hazardExamineObjectIds hazardExamineObjectIds
    */
    @Override
    @Transactional
    public void physicallyDeleteHazardExamineObject(String hazardExamineObjectIds) {
        List<String> list = Arrays.asList(hazardExamineObjectIds.split(StringPool.COMMA));
        LambdaQueryWrapper<HazardExamineObject> wapper = new LambdaQueryWrapper<>();
        wapper.in(HazardExamineObject::getExamineObjectId,list);
        this.remove(wapper);
    }

    /**
     * 根据安全检查examine_id查询
     * @author admin
     * @param examineIds
     * @return List<HazardExamineRecordDto>
     */
    @Override
    public List<HazardExamineObjectDto> findByExamineIds(List<String> examineIds) {
        List<HazardExamineObjectDto> result = new ArrayList<>();
        LambdaQueryWrapper<HazardExamineObject> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(HazardExamineObject::getDeletedFlg, FebsConstant.VALID);
        queryWrapper.in(HazardExamineObject::getExamineId,examineIds);
        List<HazardExamineObject> hazardExamineObjects = this.baseMapper.selectList(queryWrapper);
        if(CollectionUtils.isNotEmpty(hazardExamineObjects)){
            result = BeanHelper.copyWithCollection(hazardExamineObjects,HazardExamineObjectDto.class);
        }
        return result;
    }

    /**
     * 根据安全检查examine_id删除
     * @author admin
     * @param examineIds
     * @return List<HazardExamineRecordDto>
     */
    @Override
    public void deleteByExamineIds(String examineIds) {
        List<String> list = Arrays.asList(examineIds.split(StringPool.COMMA));
        LambdaQueryWrapper<HazardExamineObject> wapper = new LambdaQueryWrapper<>();
        wapper.in(HazardExamineObject::getExamineId,list);
        this.remove(wapper);
    }

}
